package src.cs2340.todoApp;

public class Account {
	private String password;
	public String username;
	public String name;
	public String email;
	public Database database;    //why do we need an instance of db in every single account?
	public boolean loginFlag;
	public TaskList tasksList;
	
	/**
	 * Constructor for creating a new account
	 * @param user username
	 * @param pass password
	 * @param name name of the user
	 * @param email email of the user
	 * @param db database instance
	 */
	protected Account(String user, String pass, String name, String email, Database db){
		password = pass;
		username = user;
		database = db;
		this.name = name;
		this.email = email;
		this.tasksList = new TaskList();
		
	}
/**
 * Try to add the account to the databse
 * @throws AddAccountException
 */
	protected void register() throws AddAccountException{
		database.addAccount(this);
	}
	//login is called on the individual account, and checks the database for an exact copy of that account, and sets
	//the login flag based on whether they "should" be logged in or not.
	/**
	 * set the login flag to the result of the login, flag will be used later to determine login state.
	 * @return
	 */
	protected boolean login(){
		loginFlag = database.login(this);
		return loginFlag;
	}
	//This sets the loginflag to be false, which will, in effect, log the user out. 
	/**
	 * logs you out be changing login to false.
	 * @return
	 */
	protected boolean logout(){
		loginFlag = false;
		return loginFlag;
	}
	/**
	 * getter
	 * @return
	 */
	protected String getName(){
		return name;
	}
	/**
	 * setter
	 * @param name
	 */
	protected void setName(String name){
		this.name = name;
	}
	/**
	 *  Check for equality
	 * @param usrName
	 * @return
	 */
	public boolean verifyUsername(String usrName) {
		return usrName.equals(this.username);
	}
	/**
	 * check for equality
	 * @param pswd
	 * @return
	 */
	public boolean verifyPassword(String pswd) {
		return pswd.equals(this.password);
	}
	
	
	
	/**
	 * remade equality for an account class (checks username/password)
	 * @param
	 */
	@Override
	public boolean equals(Object obj) {
		Account otherAccount = (Account) obj;
		if (otherAccount.verifyUsername(this.username)) {
			if (otherAccount.verifyPassword(this.password)) {
				return true;
			}
		}
		return false;
		
	}
	
	/**
	 * Adds a task item in the accounts task list
	 * @param item
	 */
	public void addTaskItem(TaskItem item) {
		tasksList.createTask(item);
	}
	
	/**
	 * To String method for Account class
	 */
	@Override
	public String toString() {
		return username;
	}
}
